<template>
	<view class="page">
		<view class="header">
			<span class="title">首页</span>	
			<uni-icons type="loop" size="20" class="loop-btn" color="#666EE8" @click="gotobabylist"></uni-icons>&nbsp;
			<span class="switch-account" @click="gotobabylist">切换账号</span>
		</view>
		
		<view class="container">
			
		    <image class="avatar" :src="avatar" mode="aspectFill"  @click="gotobabyinformation"></image>
			
			<h3 class="baby-name" @click="gotobabyinformation">{{ nickname }}</h3>
			<view class="baby-age" @click="gotobabyinformation">
				<text class="baby-age-item">{{ birthday }}</text>
				<uni-icons type="compose" size="20"></uni-icons>
			</view>
		</view>
		
		<view class="all-item">
			<view @click="entergrowth">
				<image class="item" src="../../static/home/growth.png" mode="aspectFill"></image>
			</view>
			<view @click="enterdiary">
				<image class="item" src="../../static/home/diary.png" mode="aspectFill"></image>
			</view>
			<view @click="entervaccination">
				<image class="item" src="../../static/home/vaccination.png" mode="aspectFill"></image>
			</view>	
		</view>
		
		<view class="all-item-text">
			<text @click="entergrowth">成长记录</text>
			<text @click="enterdiary">宝宝日记</text>
			<text @click="entervaccination">疫苗接种</text>
		</view>
		
		
		<view class="first-row">
			<view class="first-row-left">
				<uni-icons type="smallcircle" size="22" color="#666EE8"></uni-icons>
				<span class="record-time">今天</span>
			</view>	
			<view class="first-row-right">第&nbsp;<span class="days">666</span>&nbsp;天</view>
		</view>
		
		<view class="my-card-border">
			<view class="my-card">		
				<view class="row-item" >
					<image src="../../static/picture/1.jpg" class="diary-image" ></image>
					<image src="../../static/picture/2.jpg" class="diary-image" ></image>
					<image src="../../static/picture/3.jpg" class="diary-image" ></image>	
				</view>
				<view class="row-item" >
					<image src="../../static/picture/4.jpg" class="diary-image" ></image>
					<image src="../../static/picture/5.jpg" class="diary-image" ></image>
					<image src="../../static/picture/6.jpg" class="diary-image" ></image>	
				</view>
				<view class="diary-content">今天是初一，带宝贝去看电影《小猪佩奇过大年》，之后去迪卡侬，本来是要给她买轮滑鞋的。但她过去尝试打棒球、踢球，之后去打乒乓球、打羽毛球。我们都试了，我感觉要了好大劲教她......</view>
				<view class="diary-content-all" @click="toDiaryContent">全文</view>
				<view class="diary-content-time">3小时前</view>
			</view>
		</view>
		
		<view class="first-row">
			<view class="first-row-left">
				<uni-icons type="smallcircle" size="22" color="#666EE8"></uni-icons>
				<span class="record-time">昨天</span>
			</view>		
			<view class="first-row-right">第&nbsp;<span class="days">665</span>&nbsp;天</view>
		</view>
		
		<view class="my-card-border">
			<view class="my-card">				
				<view class="diary-record">身高&nbsp;<text class="diary-record-data">55&nbsp;cm</text></view>
				<view class="diary-record">体重&nbsp;<text class="diary-record-data">15&nbsp;kg</text></view>
				<view class="diary-record">头围&nbsp;<text class="diary-record-data">30&nbsp;cm</text></view>
				<view class="diary-content-time">08:08</view>
			</view>
		</view>
		
		<view class="first-row">
			<view class="first-row-left">
				<uni-icons type="smallcircle" size="22" color="#666EE8"></uni-icons>
				<span class="record-time">2024/06/06</span>
			</view>		
			<view class="first-row-right">第&nbsp;<span class="days">611</span>&nbsp;天</view>
		</view>
		
		<view class="my-card-border">
			<view class="my-card">		
				<view class="my-card-title">宝宝第一次走路</view>
				<view class="diary-content">宝宝终于迈出第一步啦，开心！！！</view>
				<view class="diary-content-time">13:14</view>
			</view>
		</view>
		
		<button class="floating-button" @click="adddiary">+</button>
		
	</view>
	
	
</template>

<script setup>
import { ref,onMounted } from 'vue';

// 响应式数据
const avatar = ref('');
const nickname = ref('');
const birthday = ref('');
const height = ref('');
const weight = ref('');

onMounted(() => {
    initData();
});

// 初始化数据
const initData = async () => {
    uni.request({
		method: 'GET',
		url: 'http://localhost:9100/api/baby/selectById',
		data: {
			babyId: uni.getStorageSync('babyId')
		},
		header: {
			'bece_token': uni.getStorageSync('bece_token') 
		},
		success: (res) => {
			console.log(res.data);
			if (res.data.code == 200) {
				const babyData = res.data.data;
				avatar.value = babyData.avatar || '';
				nickname.value = babyData.nickname || '';
				birthday.value = calculateAge(babyData.birthday);
				height.value = babyData.height || '';
				weight.value = babyData.weight || '';
				uni.showToast({
					title: '加载成功',
					duration: 2000
				});
				
			} else {
				uni.showToast({
					title: res.data.msg,
					duration: 2000
				});
			}
		}
	});
};

// 计算宝宝年龄
const calculateAge = (birthday) => {
	if (!birthday) return '';

	const birth = new Date(birthday);
	const now = new Date();

	let months = (now.getFullYear() - birth.getFullYear()) * 12;
	months += now.getMonth() - birth.getMonth();

	if (months < 12) {
		return `${months}个月`;
	} else {
		const years = Math.floor(months / 12);
		const remainingMonths = months % 12;
		return remainingMonths > 0 ? 
		  `${years}岁${remainingMonths}个月` : 
		  `${years}岁`;
	}
};

const gotobabylist = () => {
	uni.navigateTo({
		url: '/pages/baby/list/list'
	})
}

const gotobabyinformation = () => {
	uni.navigateTo({
		url: '/pages/baby/updateinfo/updateinfo'
	})
}

const adddiary = () => {
	uni.navigateTo({
		url: '/pages/adddiary/adddiary'
	})
}

const entergrowth = () => {
	uni.navigateTo({
		url: '/pages/baby/growth_record_height/growth_record_height'
	})
}

const enterdiary = () => {
	uni.navigateTo({
		url: '/pages/baby_diary/diary/diary'
	})
}

const entervaccination = () => {
	uni.navigateTo({
		url: '/pages/vaccination/list/list'
	})
}

const toDiaryContent = () => {
	uni.navigateTo({
		url: '/pages/baby_diary/content/content'
	})
}
	
</script>

<style scoped>
.page{
	position: relative; 
	padding-bottom: 60px;
}
.header {
	background-color: #fff;
	padding: 25rpx 0rpx;
	border-bottom: 2rpx solid #F5F5F5;
	position: relative;
}
.title {
	font-size: 46rpx;
	font-weight: 900;
	padding-left: 30rpx;
	
}
.loop-btn{
	position: absolute;
	top: 38rpx;
	right: 150rpx;
}
.container{
	background-color: #F2F2F2;
	padding-top: 40rpx;
}
.avatar {
	width: 80px; 
	height: 80px;
	border-radius: 50%;
	display: block;
	margin: auto;
	background-color: #999999; 
	border: 10rpx solid #ffffff;
}

.baby-name{
	text-align: center;
	margin: 20rpx 0;
}
.baby-age{
	text-align: center;
	padding-bottom: 40rpx;
}
.baby-age-item{
	font-size: 26rpx;
	color: #999999;
	
}
.all-item{
	display: flex;
	justify-content: space-around; 
	margin-top: 50rpx;
}
.item {
	width: 50px; 
	height: 50px;
	border-radius: 50%;
	background-color: #fff; 
}
.all-item-text{
	display: flex;
	justify-content: space-around; 
	margin-top: 10rpx;
	margin-bottom: 40rpx;
	font-size: 29rpx;
}

.first-row{
	margin: 0rpx 24rpx;
	display: flex;
	justify-content: space-between;
	
}
.first-row-left{
	display: flex;
	align-items: center;
}
.record-time{
	font-size: 34rpx;
	margin-left: 20rpx;
	margin-bottom: 4rpx;
}
.first-row-right{
	font-size: 26rpx;
	margin-bottom:2rpx;
	color: #999999;
	
}
.days{
	font-size: 38rpx;
	font-weight: 800;
	color: #666EE8;
}  
.my-card-border{
	border-left: 4rpx solid #E4E4E4;
	margin-left: 44rpx;
	margin-bottom: 30rpx;
}

.my-card{
	box-shadow: 0rpx 0rpx 4rpx rgb(170, 170, 127);
	margin: 20rpx 30rpx ;
	background-color: #fff;
	border-radius: 10rpx;
	padding: 20rpx ;
}
.row-item{
	display: flex;
	justify-content: space-around; 
	margin-bottom: 20rpx;
}

.diary-image{
	width: 180rpx;
	height: 110rpx;
}
.diary-content{
	font-size: 26rpx;
	line-height: 40rpx;
	margin-bottom: 20rpx;
	color: #999999;
}
.diary-content-all{
	font-size: 26rpx;
	line-height: 50rpx;
	color: #666EE8;
	display: flex;
	justify-content: flex-end;
}
.diary-content-time{
	font-size: 24rpx;
	line-height: 50rpx;
	color: #DBD0CC;
}
.diary-record{
	font-size: 30rpx;
	line-height: 50rpx;
	color: #999999;
}
.diary-record-data{
	color:black;
}
.my-card-title{
	font-size: 28rpx;
	margin:30rpx 0rpx;
}
.switch-account {
	font-size: 28rpx;
	color: blue;
	position: absolute;
	top: 38rpx;
	right: 30rpx;
}
.floating-button {
	position: fixed; 
	bottom: 180rpx; 
	right: 60rpx; 
	z-index: 1000; 
	width: 80rpx; 
	height: 80rpx;
	border-radius: 50%; 
	background-color: #666EE8; 
	box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.2);
	color: white; 
	font-size: 50rpx; 
	line-height: 80rpx;
	display: flex;
	justify-content: center;
}
</style>
